package com.sharp.socket.io.service.chat;

import com.sharp.authorization.dao.common.entity.AcUser;
import com.sharp.authorization.dao.common.repository.AcUserService;
import com.sharp.authorization.service.token.TokenService;
import com.sharp.kernel.constant.CommonConst;
import com.sharp.socket.io.dao.msyql.entity.ChatRoomFriend;
import com.sharp.socket.io.dao.msyql.repository.ChatRoomFriendService;
import com.sharp.socket.io.vo.chat.friend.ChatFriendRes;
import com.sharp.socket.io.vo.chat.friend.ConfirmFriendReq;
import com.sharp.socket.io.vo.chat.friend.MakeFriendReq;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Title: ChatFriendManageService
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2021/2/7 16:46
 * @description Project Name: Seed
 * @Package: com.srct.service.socketio.service.chat
 */
@Service
public class ChatFriendManageService {

    @Resource
    private TokenService tokenService;
    @Resource
    private AcUserService acUserService;
    @Resource
    private ChatRoomFriendService chatRoomFriendService;

    public List<ChatFriendRes> getFriendList() {
        String userId = tokenService.getCurrentUserId();
        List<ChatRoomFriend> friendList = chatRoomFriendService.getFriendByUserId(userId);
        return buildChatFriendRes(friendList);
    }

    private List<ChatFriendRes> buildChatFriendRes(List<ChatRoomFriend> friendList) {
        List<ChatFriendRes> resList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(friendList)) {
            for (ChatRoomFriend friend : friendList) {
                AcUser user = acUserService.getUserById(friend.getFriendId());
                ChatFriendRes res = buildChatFriendRes(user);
                resList.add(res);
            }
        }
        return resList;
    }

    private ChatFriendRes buildChatFriendRes(AcUser user) {
        ChatFriendRes res = new ChatFriendRes();
        res.setUserId(user.getId());
        res.setUserName(user.getName());
        res.setAvatar(user.getAvatar());
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    public void friend(MakeFriendReq req) {
        String userId = tokenService.getCurrentUserId();
        ChatRoomFriend chatFriend = buildChatFriend(userId, req.getFriendId());
        chatRoomFriendService.insertOrUpdateSelective(chatFriend);
    }

    private ChatRoomFriend buildChatFriend(String userId, String friendId) {
        ChatRoomFriend friend = new ChatRoomFriend();
        friend.setUserId(userId);
        friend.setFriendId(friendId);
        return friend;
    }

    public List<ChatFriendRes> getUnconfirmedFriend() {
        String userId = tokenService.getCurrentUserId();
        List<ChatRoomFriend> friendList = chatRoomFriendService.getUnconfirmedFriendByUserId(userId);
        return buildChatFriendRes(friendList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void confirmFriend(ConfirmFriendReq req) {
        String userId = tokenService.getCurrentUserId();
        ChatRoomFriend chatFriend = buildChatFriend(req.getFriendId(), userId);
        chatFriend.setConfirm(req.getConfirmed());
        chatFriend.setConfirmAt(new Date());
        chatRoomFriendService.insertOrUpdateSelective(chatFriend);
        if (StringUtils.equals(req.getConfirmed(), CommonConst.YES)) {
            chatFriend.setUserId(userId);
            chatFriend.setFriendId(req.getFriendId());
            chatRoomFriendService.insertOrUpdateSelective(chatFriend);
        }
    }
}
